package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    @Override
    public List<Setmeal> search(Setmeal setmeal) {
        return setmealMapper.search(setmeal);
    }
    @Override
    public void insert(SetmealDTO setmealDTO) {
        Setmeal setmeal =Setmeal.builder().id(setmealDTO.getId()).
                categoryId(setmealDTO.getCategoryId()).
                name(setmealDTO.getName()).
                price(setmealDTO.getPrice()).
                status(setmealDTO.getStatus()).
                description(setmealDTO.getDescription()).
                image(setmealDTO.getImage()).createTime(LocalDateTime.now()).build();
        setmealMapper.insert(setmeal);
        Long id = setmeal.getId();
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        if(!CollectionUtils.isEmpty(setmealDishList)){
            setmealDishList.forEach(e->e.setSetmealId(id));
        }
        setmealDishMapper.insertBatch(setmealDishList);
    }

    @Override
    public PageResult getByPage(SetmealPageQueryDTO x) {
        PageHelper.startPage(x.getPage(), x.getPageSize());
        Page<SetmealVO> p = (Page<SetmealVO>) setmealMapper.getByPage(x);
        return new PageResult(p.getTotal(),p.getResult());
    }

    @Override
    public SetmealVO getById(Long id) {
        return setmealMapper.getById(id);
    }

    @Override
    public void delete(List<Long> ids) {
        //遍历集合拿到setmealVO
        for(Long id:ids){
            SetmealVO setmealVO = setmealMapper.getById(id);
            //拿到套餐状态
            if(setmealVO.getStatus()==1){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        setmealMapper.delete(ids);
        setmealDishMapper.deleteBatch(ids);
    }

    @Override
    @Transactional
    public void startOrStop(Integer status, Long id) {
        //通过ID查看套餐中dish的状态 如果有停售的菜品则无论status如何直接让套餐停售。否则如果status等于1就进行更新赋值
        SetmealVO setmealVO = setmealMapper.getById(id);
        List<SetmealDish> setmealDishList = setmealVO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishList) {
            //这里只能查出dishId所以要去dish表里面找状态
            DishVO dishVO = dishMapper.getById(setmealDish.getDishId());
            if(dishVO.getStatus()==0){
                setmealMapper.startOrStop(0,id);
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        setmealMapper.startOrStop(status,id);
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
            Setmeal setmeal = Setmeal.builder().id(setmealDTO.getId()).
                    categoryId(setmealDTO.getCategoryId()).
                    name(setmealDTO.getName()).
                    price(setmealDTO.getPrice()).
                    status(setmealDTO.getStatus()).
                    description(setmealDTO.getDescription()).
                    image(setmealDTO.getImage()).build();
            List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
            setmealMapper.update(setmeal);
            setmealDishMapper.deleteBatch(Arrays.asList(setmeal.getId()));
            if(!CollectionUtils.isEmpty(setmealDishList)){
                setmealDishList.forEach(e->e.setSetmealId(setmeal.getId()));
            }
            setmealDishMapper.insertBatch(setmealDishList);
    }
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
